# !/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from loguru import logger


def send_csv(msg_title, csv_path):
    # 邮箱服务器地址
    # 发件人邮箱地址
    # 发件人邮箱密码(使用申请的客户端授权码代替)
    smtp_server = 'smtp.qq.com'
    from_addr = '83852084@qq.com'
    passwd = 'ruqbqiqjmnjhbgih'

    # 收件人邮箱地址
    to_addr = 'stockbing@icloud.com'

    # 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)
    msg = MIMEMultipart()
    # 构造附件1，传送当前目录下的 ces.xlsx 文件
    att1 = MIMEText(open(csv_path, 'rb').read(), 'base64', 'utf-8')
    # 这里的filename可以任意写，写什么名字，邮件中显示什么名字
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="temp.csv"'
    msg.attach(att1)

    # 设置邮件主题
    msg['Subject'] = Header(msg_title, charset='utf-8')
    msg['from'] = from_addr
    msg['to'] = to_addr

    # 获取SMTP对象
    server = smtplib.SMTP_SSL(host=smtp_server, port=465)
    # 登录163邮箱服务器
    server.login(user=from_addr, password=passwd)
    # 发送邮件
    server.sendmail(from_addr=from_addr, to_addrs=to_addr, msg=msg.as_string())
    # 退出邮箱服务器
    server.quit()
    logger.info(f'{msg_title}-邮件发送成功')


if __name__ == '__main__':
    msg_title = 'csv_test'
    csv_path = '../main_all_txt2/1003光伏_all_6.csv'
    send_csv(msg_title, csv_path)
